define("zui/pagebar",["zui/utils"],function(require,exports,module){

		zUtil=require("zui/utils");

		var pagebarTpl="<ul class='z-pagebar'><li><a href='javascript:;' class='prev-page'>{prevtext}</a></li><li><input type='number' value='1' class='pagebar-input' /></li><li><a href='javascript:;' class='next-page'>{nexttext}</a></li></ul>"

		function zPageBar(options,callback){
				this.pagebarTpl=pagebarTpl;
				this.pagestate="";
				this.pageOptions={
						prevPageIndex:0,
						nextPageIndex:0,
						currentPageIndex:0,
						totalPageIndex:0
				}
				
				this._init(options,callback);
		}

		zPageBar.prototype={

				_init:function(options,callback){
						var _self=this;
						if(!options&&!options.srcNode)
					 		return;
					 	_self.el=$(options.srcNode);
					 	_self.pageOptions=$.extend(_self.pageOptions,options.pageOptions||{});
					 	if(zUtil.isFunction(callback))
					 		_self.callback=callback;
					 	_self._setTemplate(options);
				},
				_setTemplate:function(options){
						var _self=this,
						 _prevtext="上一页",
						 _nexttext="下一页";
						if(options.prevtext)
							_prevtext=options.prevtext;
						if(options.nexttext)
							_nexttext=options.nexttext;
						var template=zUtil.substitute(_self.pagebarTpl,{prevtext:_prevtext,nexttext:_nexttext});
						_self.el.html(template);
						_self._handleEvent();


				},
				refresh:function(){
						var _self=this;
						switch(_self.pagestate){

							case "prev":
										_self.pageOptions.currentPageIndex=_self.pageOptions.prevPageIndex;
										$(".pagebar-input",_self.el).val(_self.pageOptions.currentPageIndex);
										break;

							case "next":
										_self.pageOptions.currentPageIndex=_self.pageOptions.nextPageIndex;
										$(".pagebar-input",_self.el).val(_self.pageOptions.currentPageIndex);
										break;
							case "hander":
										$(".pagebar-input",_self.el).val(_self.pageOptions.currentPageIndex);
										break;
						}

				},
				_handleEvent:function(){

						var _self=this;
						if(_self.callback){


							$(".prev-page",_self.el).on("click",function(ev){
										_self.pagestate="prev";
										 _self.pageOptions.prevPageIndex=parseInt(_self.pageOptions.currentPageIndex-1)>0?parseInt(_self.pageOptions.currentPageIndex-1):1;
										_self.callback({pageIndex:_self.pageOptions.prevPageIndex})
										
							});

							$(".next-page",_self.el).on("click",function(ev){
										_self.pagestate="next";
										_self.pageOptions.nextPageIndex=(parseInt(_self.pageOptions.currentPageIndex+1)-parseInt(_self.pageOptions.totalPageIndex))>0?parseInt(_self.pageOptions.totalPageIndex):parseInt(_self.pageOptions.currentPageIndex+1);
										_self.callback({pageIndex:_self.pageOptions.nextPageIndex})
										
							});

							$(".pagebar-input",_self.el).on("blur",function(ev){
										_self.pagestate="hander";
										var  value=parseInt($(this).val());
										 if(!isNaN(value)&&zUtil.isNumber(value)){
										 		_self.pageOptions.totalPageIndex<value?_self.pageOptions.currentPageIndex=_self.pageOptions.totalPageIndex:_self.pageOptions.currentPageIndex=value;
												_self.callback({pageIndex:_self.pageOptions.currentPageIndex});
												
										 }

							});

						}




				}



		}

		return zPageBar;

});